package com.fanwh.healthycampus2.fragment;


import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.fanwh.healthycampus2.R;
import com.fanwh.healthycampus2.bean.HealthyInformation;
import com.fanwh.healthycampus2.bean.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class InquireFragment extends Fragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView temperature;
    private TextView date;
    private TextView studentID;
    private TextView name;
    //private TextView school;
    //private TextView tel;



    public InquireFragment() {
        // Required empty public constructor
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x101:
                    if (swipeRefreshLayout.isRefreshing()){
                        swipeRefreshLayout.setRefreshing(false);//设置不刷新
                    }
                    break;
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_inquire, container, false);

        swipeRefreshLayout = inflate.findViewById(R.id.swip);
        temperature = inflate.findViewById(R.id.tv);
        date = inflate.findViewById(R.id.inquire_date);
        studentID = inflate.findViewById(R.id.inquire_studentID);
        name = inflate.findViewById(R.id.inquire_name);

        //toolbar = inflate.findViewById(R.id.toolbar_inquire);




        initData();

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new LoadDataThread().start();
            }
        });
        return inflate;
    }

    private void initData() {
        User user = BmobUser.getCurrentUser(User.class);
        String username = (String) BmobUser.getObjectByKey("username");
        BmobQuery<HealthyInformation> query = new BmobQuery<HealthyInformation>();
        query.addWhereEqualTo("student",user);
        String sql = "SELECT * From HealthyInformation where studentID = '"+username+"'ORDER BY createdAt DESC LIMIT 1";
        query.doSQLQuery(sql, new SQLQueryListener<HealthyInformation>() {
            @Override
            public void done(BmobQueryResult<HealthyInformation> bmobQueryResult, BmobException e) {
                if(e == null){
                    List<HealthyInformation> list = bmobQueryResult.getResults();
                    //Toast.makeText(getActivity(),"查询成功：共"+list.size()+"条数据",Toast.LENGTH_SHORT).show();
                    String str = " ";
                    for(HealthyInformation heathyInformation : list){
                        temperature.setText(heathyInformation.getTemperature());
                        date.setText(heathyInformation.getCreatedAt());
                        studentID.setText(heathyInformation.getStudentID());
                        name.setText(heathyInformation.getName());

                        if(Double.parseDouble(heathyInformation.getTemperature()) < 37.3){
                            temperature.setTextColor(getResources().getColor(R.color.colorPrimaryDarkBlack));
                            //Toast.makeText(getContext(),"体温正常，不用太过担心",Toast.LENGTH_LONG).show();
                        }else {
                            temperature.setTextColor(getResources().getColor(R.color.red));
                            //Toast.makeText(getActivity(),"体温异常，建议您去医院瞧瞧",Toast.LENGTH_LONG).show();
                        }


                    }

                }else {
                    System.out.println(e.getErrorCode());
                }
            }
        });
    }

    class LoadDataThread extends Thread{
        @Override
        public void run(){
            inquireData();
            try {
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            handler.sendEmptyMessage(0x101);
        }

        private void inquireData() {
            User user = BmobUser.getCurrentUser(User.class);
            String username = (String) BmobUser.getObjectByKey("username");
            BmobQuery<HealthyInformation> query = new BmobQuery<HealthyInformation>();
            query.addWhereEqualTo("student",user);
            String sql = "SELECT * From HealthyInformation where studentID = '"+username+"'ORDER BY createdAt DESC LIMIT 1";
            query.doSQLQuery(sql, new SQLQueryListener<HealthyInformation>() {
                @Override
                public void done(BmobQueryResult<HealthyInformation> bmobQueryResult, BmobException e) {
                    if(e == null){
                        List<HealthyInformation> list = bmobQueryResult.getResults();
                        //Toast.makeText(getActivity(),"查询成功：共"+list.size()+"条数据",Toast.LENGTH_SHORT).show();
                        String str = " ";
                        for(HealthyInformation heathyInformation : list){
                            temperature.setText(heathyInformation.getTemperature());
                            date.setText(heathyInformation.getCreatedAt());
                            studentID.setText(heathyInformation.getStudentID());
                            name.setText(heathyInformation.getName());

                            if(Double.parseDouble(heathyInformation.getTemperature()) < 37.3){

                                Toast.makeText(getContext(),"体温正常，不用太过担心",Toast.LENGTH_LONG).show();
                            }else {
                                temperature.setTextColor(getResources().getColor(R.color.red));
                                //Toast.makeText(getActivity(),"体温异常，建议您去医院瞧瞧",Toast.LENGTH_LONG).show();
                                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                builder.setTitle("警告");
                                builder.setIcon(R.mipmap.warning);
                                builder.setMessage("体温异常，建议您去医院瞧瞧!!!");
                                builder.setPositiveButton("好的",null);
                                builder.show();
                            }
                        }

                    }else {
                        System.out.println(e.getErrorCode());
                    }
                }
            });
        }
    }

}
